package ext.generic.wfaugment;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.ServletRequest;

import wt.fc.Persistable;
import wt.fc.ReferenceFactory;
import wt.fc.WTObject;

import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;

import wt.project.Role;

import wt.session.SessionServerHelper;

import wt.team.Team;
import wt.team.TeamManaged;
import wt.team.TeamReference;

import wt.util.WTException;
import wt.util.WTProperties;

import wt.workflow.definer.WfAssignedActivityTemplate;
import wt.workflow.engine.WfActivity;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WorkItem;

import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.mvc.components.AbstractComponentBuilder;
import com.ptc.mvc.components.ColumnConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfig;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.components.TableConfig;
import com.ptc.mvc.util.ClientMessageSource;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.beans.NmHelperBean;

@ComponentBuilder("generic.setParticipants")
public class WorkFlowSetParticipantsTableBuilder extends AbstractComponentBuilder
{
    private static boolean VERBOSE = false;
    public static String IBA_PRODUCT_RECORD_NUMBER = "ProductRecordsNumber";
    public static WTProperties wtProperties;
    private final ClientMessageSource messageSource = getMessageSource("ext.generic.wfaugment.workflowStepRB");
    static
    {
        try
        {
            wtProperties = WTProperties.getLocalProperties();
            VERBOSE = wtProperties.getProperty("ext.generic.wfaugment.verbose", false);
        }
        catch (Throwable throwable)
        {
            throw new ExceptionInInitializerError(throwable);
        }
    }

    public WorkFlowSetParticipantsTableBuilder()
    {
    }

    public ArrayList buildComponentData(ComponentConfig componentconfig, ComponentParams componentparams) throws WTException
    {
        boolean flag = SessionServerHelper.manager.setAccessEnforced(false);
        ArrayList arraylist = new ArrayList();
        try
        {
            NmHelperBean nmhelperbean = ((JcaComponentParams) componentparams).getHelperBean();
            NmCommandBean nmcommandbean = nmhelperbean.getNmCommandBean();
            NmOid nmoid = nmcommandbean.getActionOid();
            if (nmoid == null)
                nmoid = nmcommandbean.getPrimaryOid();
            if (VERBOSE)
                System.out.println(">>> nmoid  " + nmoid);
            WTObject contextObj = (WTObject) nmoid.getWtRef().getObject();
            if (contextObj instanceof WorkItem)
            {
                WorkItem workItem = (WorkItem) contextObj;
                WfActivity wfactivity = (WfActivity) workItem.getSource().getObject();
                WfProcess wfprocess = wfactivity.getParentProcess();
                Team team = (Team) ((TeamReference) ((TeamManaged) wfprocess).getTeamId()).getObject();
                String[] roletype = (String[]) nmcommandbean.getParameterMap().get("roletype");
                Role role = Role.toRole(roletype[0]);
                Enumeration userem = team.getPrincipalTarget(role);
                if (userem != null && userem.hasMoreElements())
                {
                    while (userem.hasMoreElements())
                    {
                        WTPrincipalReference principalRef = (WTPrincipalReference) userem.nextElement();
                        WTPrincipal wtpr = principalRef.getPrincipal();
                        arraylist.add(wtpr);
                    }
                }
                //添加签审表里面配置的角色在上下文团队中的成员
                Persistable persistable = null;
                if (workItem.getPrimaryBusinessObject() != null)
                {
                    persistable = workItem.getPrimaryBusinessObject().getObject();
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + persistable);
                }
                if (persistable != null)
                {
                    String[] aciveoid = (String[]) nmcommandbean.getParameterMap().get("activeoid");
                    if (VERBOSE)
                        System.out.println(">>>  aciveoid == " + aciveoid[0]);
                    //activeoid查找工作模板
                    ReferenceFactory referencefactory = new ReferenceFactory();
                    WfAssignedActivityTemplate wfatp = (WfAssignedActivityTemplate) referencefactory.getReference(aciveoid[0]).getObject();
                    Hashtable activehtInguide = WorkFlowStepUtil.getWorkFlowStepInReviewGuideByName(workItem.getPrimaryBusinessObject().getObject(), wfprocess);
                    String rolestr = (String) activehtInguide.get(wfatp.getName());
                    ArrayList arraylist2 = WorkFlowStepUtil.getUserInTheContainerTeam(persistable, rolestr);
                    if (arraylist2 != null && arraylist2.size() > 0)
                        arraylist.addAll(arraylist2);
                }

            }
        }
        finally
        {
            SessionServerHelper.manager.setAccessEnforced(flag);
        }

        return arraylist;
    }

    public ComponentConfig buildComponentConfig(ComponentParams componentparams) throws WTException
    {
        NmHelperBean nmhelperbean = getHelperBean(componentparams);
        setAttribute(nmhelperbean.getRequest(), "showContextInfo", "false");
        TableConfig tableconfig = newTableConfig();
        tableconfig.setType(WTPrincipal.class.getName());
        tableconfig.setLabel(getMessage("SET_PARTICIPANTS"));
        tableconfig.setSelectable(true);
        tableconfig.setId("setParticipantsTable");
        tableconfig.setActionModel("Generic SetParticipants Tool Bar");
        ColumnConfig columnconfig = newColumnConfig("name", true);
        columnconfig.setDataUtilityId("name");
        tableconfig.addComponent(columnconfig);
        ColumnConfig columnconfig2 = newColumnConfig("setPrincipalFullName", true);
        columnconfig2.setDataUtilityId("setPrincipalFullName");
        columnconfig2.setLabel(getMessage("FULL_NAME"));
        tableconfig.addComponent(columnconfig2);
        ColumnConfig columnconfig3 = newColumnConfig("wtprincipal_role", true);
        columnconfig3.setDataUtilityId("wtprincipal_role");
        columnconfig3.setLabel(getMessage("ROLE_BELONG"));
        tableconfig.addComponent(columnconfig3);

        tableconfig.setShowCount(true);
        tableconfig.setShowCustomViewLink(false);
        return tableconfig;
    }

    protected NmHelperBean getHelperBean(ComponentParams componentparams)
    {
        return ((JcaComponentParams) componentparams).getHelperBean();
    }

    protected void setAttribute(ServletRequest servletrequest, String s, Object obj)
    {
        servletrequest.setAttribute(s, obj);
    }

    protected TableConfig newTableConfig()
    {
        return getComponentConfigFactory().newTableConfig();
    }

    protected ColumnConfig newColumnConfig(String s, boolean flag)
    {
        return getComponentConfigFactory().newColumnConfig(s, flag);
    }

    protected String getMessage(String s)
    {
        return messageSource.getMessage(s);
    }

}
